package aviso.controller;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import aviso.entity.UserName;
import aviso.entity.util.JsfUtil;
import aviso.entity.util.LocaleService;
import aviso.entity.util.PasswordUtil;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

/**
 *
 * @author avgusti
 */
@ManagedBean
@SessionScoped
public class FrontController implements Serializable {

    @EJB
    private aviso.facade.UserNameFacade userFacade;
    private UserName user = new UserName();
    private long userID = 0;
    private boolean editMode = false;

    public boolean isEditMode() {
        return editMode;
    }

    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }

    public long getUserID() {
        return userID;
    }

    public void setUserID(long userID) {
        this.userID = userID;

    }

    public void setUser(UserName user) {
        this.user = user;
    }
    // @ManagedProperty (name="error", value="#{param.error}")
    private String error;

    @PostConstruct
    public void init() {
        error = "class javax.faces.application.ViewExpiredException";
        FacesContext context = FacesContext.getCurrentInstance();
        user.setLocale(context.getViewRoot().getLocale().getLanguage());


    }

    public FrontController() {
    }

    //DUMP action to submit form values
    public void submit() {
    }

    public String calcError() {
        FacesContext context = FacesContext.getCurrentInstance();
        ExternalContext ec = context.getExternalContext();
        String terror = ec.getRequestParameterMap().get("errorpage:error");
        if (terror != null) {
            error = terror;
        } else {
            terror = ec.getRequestMap().get("javax.servlet.error.exception_type").toString();
            if (terror != null) {
                error = terror;
            }
        }
        return LocaleService.getInstance().translate(error);
    }

    public UserName getUser() {
        return user;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public String logOut() {
        String lc = user.getLocale();
        user = new UserName();
        user.setLocale(lc);
        userID = 0;
        return "index?faces-redirect=true";
    }

    public boolean islogedIn() {
        if (user == null) {
            return false;
        }
        return userID > 0;
    }

    public void logIn(ValueChangeEvent e) {
        userID = (Long) e.getNewValue();
        logIn();

    }

    public void logIn() {
        UserName user_record = null;
        //dev time workaround
        if (userID != 0 && user.getId() == null) {
            user_record = userFacade.find(userID);
            user = user_record == null ? user : user_record;
        } //end dev
        else {
            if (user.getPasshash() != null) {
                user.setPasshash(PasswordUtil.genPassHash(user.getPasshash()));
                user_record = userFacade.getUserByName(user.getName());
                if (user_record == null) {
                    JsfUtil.addErrorMessage(LocaleService.getTranslation("NoSuchUser"));
                }
            }
        }
        if (user_record != null
                && user.getName().equals(user_record.getName())
                && user.getPasshash().equals(user_record.getPasshash())) {
            user = user_record;
        }
        if (user.getId() == null && user_record != null) {
            JsfUtil.addErrorMessage(LocaleService.getTranslation("PasswordDotMatch"));
        }
        if (user.getId() != null) {//we are in
            userID = user.getId();
        }
        if (user.getLocale() == null) {
            FacesContext context = FacesContext.getCurrentInstance();
            user.setLocale(context.getViewRoot().getLocale().getLanguage());
        }

    }

    public void changeLocale() {
        if (user.getId() != null) {
            userFacade.edit(user);
        }
        // locale set in template

        //FacesContext context = FacesContext.getCurrentInstance();  
        //context.getViewRoot().setLocale(new Locale(user.getLocale()));
    }

    public boolean checkLocale(String lc) {
        if (lc.equals(user.getLocale())) {
            return true;
        }
        return false;
    }
}
